Chapter 4: JavaScript and JQuery
Let’s go through some of JavaScript’s most important features to give you a better idea of how it works. The first feature is Variables. Variables are like storage containers for values. Data can be placed in these containers and then you can refer to it by naming the container. Every worthwhile programming task needs a variable. If values were unable to change, your webpage would be less interactive. You must declare your variable before you can use it in a JavaScript program. The keyword for variable (as seen in Chapter One) is var.
<script type= “text/javascript”>
<! --
var age;
var race;
// -- >
</script>
You can also declare different variables with the same keyword. It would look like this:
<script type= “text/javascript”>
<! --
var age, race;
// -- >
</script> (JavaScript - Syntax, n.d.)
Variable initialization happens when we store a value in a variable. You can add values at any point in time. We have created a variable called race, so we can add the value ‘black’ to it at a later point and we can add the value for age at the time of initialization. It would look like this:
<script type= “text/javascript”>
<! --
var age = “39”
var race;
race= black;
// -- >
</script> (JavaScript - Syntax, n.d.)
The var keyword only needs to be used once, at the time of initialization.
A variable’s scope is the area of your program where it is defined. In JavaScript, there are only two:
● Global Variables–Can be defined anywhere in your JavaScript code.
● Local Variables–Is only visible within the scope of the function in which it is defined.
Local variables take precedence over global variables with the same name within the body of a function. When a local variable or a function parameter has the same name as a global variable, the global variable is effectively hidden. For example:
<html>
<body onload = checkscope();>
<script type = “text/javascript”>
<! --
var myVar = “global”; // Global variable
function checkscope( ) {
var myVar = “local”; // Local variable
document.write(myVar);
}
// —->
</script>
<body>
<html> (JavaScript - Syntax, n.d.)
There are rules to naming your variables. The names of your variables are case sensitive, so race and Race are two different variables. Variable names cannot start with a number. They either have to start with a letter or an underscore. If your variable was named 345age, it would be invalid. But if it was named _345age it would be valid.
There is a list of JavaScript reserved keywords that cannot be used as a variable name. The list is as follows:
abstract | boolean | break | byte | case | catch |
char | class | const | continue | debugger | default |
delete | do | double | else | enum | export |
extends | false | final | finally | float | for |
function | goto | if | implements | import | in |
instanceof | int | interface | long | native | new |
null | package | private | protected | public | return |
short | static | super | switch | synchronized | this |
throw | throws | transient | true | try | typeof |
var | void | volatile | while | with | |
(JavaScript - Syntax, n.d.)
The lion’s share of applications work because of an interaction between a client and a remote server. A client refers to the end user device. The client requests data from the server, the server receives and processes the request and then the client gets the data back in a readable format. For the most part, we do need the client server model, but with JavaScript we can avoid it completely. JavaScript helps to reduce the traffic by validating forms without input from the server.
The JavaScript Engine is a program that translates source code into a language the computer understands. All browsers today come with a JavaScript engine and each engine has two components: A MemoryHeap and a Call Stack. A Memory Heap is where memory is allocated at any given time. A Call Stack processes information added to it from the script.
JavaScript Frameworks
Frameworks for JavaScript enable the language to perform at its best with minimal setup. They give developers the building blocks they need to create JavaScript applications. The building blocks are a set of code libraries. The libraries generate code that evokes specialized functionality for the sort of app being developed. The framework establishes the structure of the whole application. There are several JavaScript frameworks out there, but the most popular ones are :
Node.js | Node.js is a cross platform, open-source, back-end JavaScript runtime environment that makes use of the V8 engine to execute JavaScript code outside of a web browser. |
Vue.js | Vue.js is an open-source JavaScript framework that creates user interfaces and single page apps. It uses a model-view viewmodel (MVVM) architectural pattern. |
AngularJS | AngularJS was an open-source, front-end framework for constructing single-page apps based on JavaScript. |
Ember.js | Ember.js is an open-source, JavaScript client-side framework for constructing web apps. It enables the development of client-side applications by offering a comprehensive solution that includes data management and application flow. |
React | React is a front-end JavaScript toolkit for creating user interfaces using UI components. React can be used as a foundation to create single-page or mobile apps. |
(JavaScript - Syntax, n.d.)
Features of JQuery
HTML Manipulation
Elements in an HTML document can be manipulated because JQuery allows the user access to the DOM.
DOM Manipulation
JQuery gives you access to the DOM’s API so that you can make any necessary changes to your HTML document. You can add and subtract and rearrange elements as you please. By doing this, your web page can display updated information without you having to refresh the page a number of times.
CSS Manipulation
The JQuery library has lines of code that can be used to control and change elements of CSS and DOM.
Effects and Animation
JQuery has an animate function that makes it easier to add different effects and animations to your web page. It also lets you animate your HTML elements by manipulating the CSS. Examples of these effects include: show, hide, fade in, fade out and slide.
Ajax
Ajax stands for Asynchronous JavaScript and XML. It lets you put content from a server or a database onto your website without having to refresh the site every time. If you are visiting a website that is constantly receiving data, it would be really inconvenient to have to refresh the page each time.
Utilities
Utilities are software programs that optimize a computer’s performance. Some utility programs are good for keeping your computer virus free and others just make your desktop look good.
JQuery can be considered legacy code because it was introduced in 2006. It has stood the test of time and can be used in most applications that exist today. JQuery is used on most, if not all wordpress sites. There are a whopping 455 million of them as of 2022. It teaches you a lot about JavaScript and is the easiest coding library to use.